<template>
  <!--tabbar只显示的页面-->
  <div
    v-if="
      $route.path == '/' ||
      $route.path == '/project' ||
      $route.path == '/we' ||
      $route.path == '/vip' ||
      $route.path == '/custom'
    "
  >
    <van-tabbar
      v-model="active"
      active-color="#000"
      fixed
      inactive-color="#666C79"
      placeholder
    >
      <van-tabbar-item
        v-for="(item, index) in tabbars"
        :key="index"
        :to="item.path"
      >
        <span>{{ item.title }}</span>
        <template #icon="props">
          <img :src="props.active ? item.active : item.normal" />
        </template>
      </van-tabbar-item>
    </van-tabbar>
  </div>
</template>

<script>
export default {
  name: "tabbar",
  data() {
    return {
      active: 0,
      tabbars: [
        {
          path: "/",
          title: "首页",
          normal: require("../assets/tab/home_normal.png"),
          active: require("../assets/tab/home_active.png"),
        },
        {
          path: "/project",
          title: "项目",
          normal: require("../assets/tab/project_normal.png"),
          active: require("../assets/tab/project_active.png"),
        },
        {
          path: "/we",
          title: "我们",
          normal: require("../assets/tab/we_normal.png"),
          active: require("../assets/tab/we_active.png"),
        },
        {
          path: "/vip",
          title: "会员",
          normal: require("../assets/tab/vip_normal.png"),
          active: require("../assets/tab/vip_active.png"),
        },
        {
          path: "/custom",
          title: "客服",
          normal: require("../assets/tab/custom_normal.png"),
          active: require("../assets/tab/custom_active.png"),
        },
      ],
    };
  },
  watch: {
    //监听路由变化
    $route(to) {
      this.tabbarActive(to.path);
    },
  },
  created() {
    this.tabbarActive(this.$route.path);
  },
  methods: {
    tabbarActive(path) {
      var index = this.tabbars.map((item) => item.path).indexOf(path);
      if (index != -1) {
        this.active = index;
      }
    },
  },
};
</script>

<style scoped></style>